package black.market.main;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

public class DownloadHtml {
	public static final String LOGTAG = "DownloadHtml";
	private StringBuilder stringBuilder = null;
	private String addr = null;
	public String text = null;

	HttpResponse response = null;
	String res = null;

	public DownloadHtml(String addr) {
		// TODO Auto-generated constructor stub
		this.addr = addr;
	}

	public DownloadHtml() {
		// TODO Auto-generated constructor stub
	}

	public String httpGetTypeReturn(String address, List<NameValuePair> paramList) throws ClientProtocolException, IOException {
		HttpGet get = new HttpGet(address + URLEncodedUtils.format(paramList, "utf-8"));
		HttpClient client = new DefaultHttpClient();
		HttpParams httpParams = client.getParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
		HttpConnectionParams.setSoTimeout(httpParams, 5000);
		response = client.execute(get);
		if (response.getStatusLine().getStatusCode() == 200) {
			HttpEntity entity = response.getEntity();
			if (entity == null) {
				BlackPublic.errorLog(LOGTAG, "httpGetTypeReturn: " + response.getStatusLine().getStatusCode());
			} else {
				res = EntityUtils.toString(entity);
			}
		} else {
			int errorCode = response.getStatusLine().getStatusCode();
			BlackPublic.errorLog(LOGTAG, "httpGetTypeReturn fail Response Code: " + errorCode);
		}
		return res;
	}

	public String getWebData(String address, String sido, String gugun) {
		String res = null;
		try {
			URL url = new URL(address);
			URLConnection conn = url.openConnection();
			String params = URLEncoder.encode("sido", "utf-8") + "=" + URLEncoder.encode(sido, "euc-kr");
			params += "&" + URLEncoder.encode("gugun", "utf-8") + "=" + URLEncoder.encode(gugun, "utf-8");
			OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
			wr.write(params);
			wr.flush();
			BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			while (true) {
				res += br.readLine();
				if (br.readLine() == null) {
					BlackPublic.errorLog(LOGTAG, "getWebData readLine is null");
					break;
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return res;
	}

	public StringBuilder getStringHtml() {
		BlackPublic.debugLog(LOGTAG, "getStringHtml() address: \n" + addr);
		try {
			URL url = new URL(addr);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			if (conn != null) {
				conn.setConnectTimeout(10000);
				conn.setUseCaches(false);
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					stringBuilder = new StringBuilder();
					BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "euc-kr"));
					for (;;) {
						String line = br.readLine();
						if (line == null || line.equals("</html>")) {
							br.close();
							conn.disconnect();
							break;
						}
						if (!"".equals(line)) {
							stringBuilder.append(line + "\n");
						}
					}
					br.close();
				} else {
					BlackPublic.errorLog(LOGTAG, "HTTP_FAIL Connect ResponseCode is " + conn.getResponseCode());
					BlackPublic.errorLog(LOGTAG, "HTTP_FAIL Connect ResponseMsg is " + conn.getResponseMessage());
				}
				conn.disconnect();
			}
		} catch (Exception e) {
			// TODO: handle exception
			BlackPublic.errorLog(LOGTAG, "getStringHtml: " + "\n" + e + "\n");
		}
		return stringBuilder;
	}
}
